﻿using PigMonica.utils;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace PigMonica
{
    class Program
    {
        static double count_old = 0;
        static double count_new = 0.1;

        static void Main(string[] args)
        {
            Console.Write("请输入筛选路径，或者您直接点击回车进入默认值\n");
            string str = Console.ReadLine();//从控制台读入输入
            if (str == "")
            {
                str = "http://www.zhubajie.com/wzkf/tt5.html";
            }
            //GetContentFromUrl(str);
            Console.WriteLine("开始爬取...");
            Console.ReadKey();
            while (count_new > count_old)
            {
               // count_old = count_new;
                GetContentFromUrl(str);
            }

            Console.ReadKey();
        }

        private static void GetContentFromUrl(string str)
        {
            var content = HttpClient.request(str, null);
            string StrDate = "";
            ArrayList al = new ArrayList();

            while ((StrDate = content.ReadLine()) != null)
            {
                Regex r1 = new Regex("<table class=\"list-task\">");

                if (r1.IsMatch(StrDate))
                {
                    Console.WriteLine("当前爬虫页\n " + str + " \n当前页一共" + Count(StrDate, "list-task-title") + "个任务\n");
                    count_new = Count(StrDate, "list-task-title");
                }


            }
        }
        private static int Count(string WithinString, string search)
        {
            if (string.IsNullOrEmpty(search))
                throw new ArgumentNullException("search");
            int counter = 0;
            int index = WithinString.IndexOf(search, 0);
            while (index >= 0 && index < WithinString.Length)
            {
                counter++;
                index = WithinString.IndexOf(search, index + search.Length);
            }
            return counter;
        }
    }
}
